package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-01-23
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Page<Teacher> listPage(Integer pageNum, Integer pageSize, TeacherQuery teacherQuery) {
        Page<Teacher> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String name =teacherQuery.getName();
        Integer level=teacherQuery.getLevel();
        String joinDateBegin=teacherQuery.getJoinDateBegin();
        String joinDateEnd= teacherQuery.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)){
            //根据名称模糊查询
            queryWrapper.likeRight("name",name);
        }
        if (level!=null){
            //根据名称模糊查询
            queryWrapper.eq("level",level);
        }if (!StringUtils.isEmpty(joinDateBegin)){
            //根据名称模糊查询
            queryWrapper.ge("join_date",joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)){
            //根据名称模糊查询
            queryWrapper.le("join_date",joinDateEnd);
        }

        baseMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public Boolean saveTeacher(Teacher teacher) {
        //唯一性校验:根据提交的讲师的name值去数据库中查询记录行数,如果>0代表数据存在
        String name = teacher.getName();
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Integer count=baseMapper.selectCount(queryWrapper);
        if (count>0){
            return false;
        }
        int i = baseMapper.insert(teacher);
        if (i==0){
            return false;
        }
        return false;
    }
}
